(function () {
    var dataMap = {};

    function dataFormatter(obj) {
        var pList = ['北京', '上海', '广州'];
        var temp;
        for (var year = 2007; year <= 2016; year++) {
            var max = 0;
            var sum = 0;
            temp = obj[year];
            for (var i = 0, l = temp.length; i < l; i++) {
                max = Math.max(max, temp[i]);
                sum += temp[i];
                obj[year][i] = {
                    name: pList[i],
                    value: temp[i]
                }
            }
            obj[year + 'max'] = Math.floor(max / 100) * 100;
            obj[year + 'sum'] = sum;
        }
        return obj;
    }

    dataMap.dataLow = dataFormatter({
        //max : 60000,
        2016:[161.4,256.2,69.5],
        2015:[160.8,254.8,68.2],
        2014:[156,236.7,66.1],
        2013:[141.6,215.7,60.4],
        2012:[129.0,198.2,55.7],
        2011:[114,180.4,0],
        2010:[108,166.7,0],
        2009:[105.5,153.2,0],
        2008:[102.1,139.8,0],
        2007:[97.3,128.1,0],
    });

    dataMap.dataMiddle = dataFormatter({
        //max : 60000,
        2016:[92.4,106.2,57.5],
        2015:[93.5,103,56.7],
        2014:[89.1,101.7,55.4],
        2013:[90.3,100.4,54.6],
        2012:[91.3,102.1,50.8],
        2011:[93.1,104.4,0],
        2010:[92,105.5,0],
        2009:[88.5,106.1],
        2008:[86.5,107.2,0],
        2007:[79.3,108.7,0],
    });

    dataMap.dataHigh = dataFormatter({
        //max : 60000,
        2016:[53.1,79.5,24.5],
        2015:[52.6,78,24.1],
        2014:[51.6,75.3,22.6],
        2013:[47.4,71.5,21.2],
        2012:[42.6,67,19.5],
        2011:[38.6,62.9,0],
        2010:[35.1,59.8,0],
        2009:[32.6,56.6,0],
        2008:[29.4,53.4,0],
        2007:[22.8,50.2,0],
    });

    var option2 = {
        baseOption: {
        	color: ['#49fadf', '#28b4bd', '#0b8e96', '#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83',  '#ca8622', '#bda29a','#6e7074', '#546570', '#c4ccd3'],
            backgroundColor: '#08121c',
            timeline: {
            	backgroundColor: '#fff',
            	lineStyle: { color: '#fff', shadowColor: '#fff' },
            	itemStyle: { normal: { color: '#fff' }, emphasis: { color: '#fff' } },
            	checkpointStyle: { color: '#f4e925', borderColor: '#f4e925' },
            	controlStyle: { normal: { color: '#fff', borderColor: '#fff' }, emphasis: { color: '#fff', borderColor: '#fff' } },
                // y: 0,
                axisType: 'category',
                // realtime: false,
                // loop: false,
                autoPlay: true,
                // currentIndex: 2,
                playInterval: 1000,
                // controlStyle: {
                //     position: 'left'
                // },
                data: [
                     '2007-12-01', '2008-12-01','2009-12-01' ,'2010-12-01',
                    {
                        value:'2011-12-01',
                        tooltip: {
                            formatter: '{b} 老龄人数逐渐增加'
                        },
                        symbol: 'diamond',
                        symbolSize: 16
                    },
                    '2012-12-01', '2013-12-01', '2014-12-01','2015-12-01',
                    {
                        value: '2016-12-01',
                        tooltip: {
                            formatter: function (params) {
                                return params.name + '老龄人数达到又一个高度';
                            }

                        },
                        symbol: 'diamond',
                        symbolSize: 18
                    }
                ],
                label: {
                    formatter: function (s) {
                        return (new Date(s)).getFullYear();
                    },
                    textStyle: { color: '#fff' }
                }
            },
            title: {
                subtext: '数据来自国家统计局',
                subtextStyle: {
                    color: '#fff'
                }
            },
            tooltip: {},
            legend: {
                x: 'right',
                data: ['60~70岁老人', '70~80岁老人', '80岁以上老人'],
                selected: {
                },
                textStyle: { color: '#fff', fontSize: 26 }
            },
            calculable: true,
            grid: {
                top: 80,
                bottom: 100
            },
            xAxis: [
                {
                    'type': 'category',
                    'axisLabel': {'interval': 0, textStyle:{color:'#fff'}},
                    'data': [
                        '北京', '上海', '广州'
                    ],
                    splitLine: {show: false}
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '高龄人数（万人）',
                    nameTextStyle:{color:'#fff'},
                    'axisLabel': {textStyle:{color:'#fff'}},
                    // max: 53500
                    max: 300
                }
            ],
            series: [
                {name: '60~70岁老人', type: 'bar'},
                {name: '70~80岁老人', type: 'bar'},
                {name: '80岁以上老人', type: 'bar'},
                {
                    name: '老人年龄段占比',
                    type: 'pie',
                    center: ['75%', '35%'],
                    radius: '28%'
                }
            ]
        },
        options: [

            {
                title: {
                    text: '2007主要城市老年人口历年增长趋势',
                    textStyle: {
                            color: '#fff'
                                }
                    },
                series: [
                    {data: dataMap.dataLow['2007']},
                    {data: dataMap.dataMiddle['2007']},
                    {data: dataMap.dataHigh['2007']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2007sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2007sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2007sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2008主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2008']},
                    {data: dataMap.dataMiddle['2008']},
                    {data: dataMap.dataHigh['2008']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2008sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2008sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2008sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2009主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2009']},
                    {data: dataMap.dataMiddle['2009']},
                    {data: dataMap.dataHigh['2009']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2009sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2009sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2009sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2010主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2010']},
                    {data: dataMap.dataMiddle['2010']},
                    {data: dataMap.dataHigh['2010']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2010sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2010sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2010sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2011主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2011']},
                    {data: dataMap.dataMiddle['2011']},
                    {data: dataMap.dataHigh['2011']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2011sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2011sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2011sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2012主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2012']},
                    {data: dataMap.dataMiddle['2012']},
                    {data: dataMap.dataHigh['2012']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2012sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2012sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2012sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2013主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2013']},
                    {data: dataMap.dataMiddle['2013']},
                    {data: dataMap.dataHigh['2013']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2013sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2013sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2013sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2014主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2014']},
                    {data: dataMap.dataMiddle['2014']},
                    {data: dataMap.dataHigh['2014']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2014sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2014sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2014sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2015主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2015']},
                    {data: dataMap.dataMiddle['2015']},
                    {data: dataMap.dataHigh['2015']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2015sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2015sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2015sum']}
                        ]
                    }
                ]
            },
            {
                title: {text: '2016主要城市老年人口历年增长趋势'},
                series: [
                    {data: dataMap.dataLow['2016']},
                    {data: dataMap.dataMiddle['2016']},
                    {data: dataMap.dataHigh['2016']},
                    {
                        data: [
                            {name: '60~70岁老人', value: dataMap.dataLow['2016sum']},
                            {name: '70~80岁老人', value: dataMap.dataMiddle['2016sum']},
                            {name: '80岁以上老人', value: dataMap.dataHigh['2016sum']}
                        ]
                    }
                ]
            }
        ]
    };
    var chart3 = echarts.init(document.getElementById('chart2'), chartTheme);
    chart3.setOption(option2);
})();